package com.itcast.monkey.web.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.itcast.monkey.domain.constant.Constants;
import com.itcast.monkey.domain.page.PageBean;
import com.itcast.monkey.domain.take_delivery.Promotion;
import com.itcast.monkey.web.action.common.BaseAction;
import com.opensymphony.xwork2.ActionContext;

import freemarker.template.Configuration;
import freemarker.template.Template;

@ParentPackage("json-default")
@Scope("prototype")
@Namespace("/")
@Controller
@SuppressWarnings("all")
public class PromotionAction extends BaseAction<Promotion>{
	
	@Action(value = "promotion_pageQuery", results = { @Result(name = "success",type ="json") })
	public String pageQuery(){
		System.out.println("PromotionAction-----------------------------------------------");
		//基于webService 获取 bos_management 的活动列表信息
		PageBean<Promotion> pageBean = WebClient
				.create("http://localhost:9001/monkey_bos_management/services/promotionService/pageQuery?page="
						+ page + "&rows=" + rows)
				.accept(MediaType.APPLICATION_JSON).get(PageBean.class);
		ActionContext.getContext().getValueStack().push(pageBean);
		return SUCCESS;
	}
//	@Action(value = "promotion_Detail")
//	public String showDetail() throws Exception{
	
//		//先判断id对应html是否存在,如果存在直接返回
//		String htmlRealPath = ServletActionContext.
//				getServletContext().getRealPath("/freemarker");
//		File htmlFile = new File(htmlRealPath+"/"+model.getId()+".html");
//		// 如果html文件不存在 ，查询数据库，结合freemarker模板生成 页面
//		if(!htmlFile.exists()){
//			//配置对象,配置,模板位置
//			Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
//			configuration.setDirectoryForTemplateLoading(new File(
//					ServletActionContext.getServletContext().getRealPath(
//							"/WEB-INF/templates")));
//			System.out.println(model.getId()+"++++++++++++++++++++++++++++++");
//			//获取模板对象
//			Template template = configuration.getTemplate("promotion_detail.ftl");
//			//动态数据对象
//			Promotion promotion=  WebClient
//					.create("http://localhost:9001/monkey_bos_management/services/promotionService/promotion/"
//							+ model.getId())
//					.accept(MediaType.APPLICATION_JSON).get(Promotion.class);
//					.create(Constants.BOS_MANAGEMENT_URL
//							+ "/monkey_bos_management/services/promotionService/promotion/"
//							+ model.getId()).accept(MediaType.APPLICATION_JSON)
//					.get(Promotion.class);
//			HashMap<String, Object> map = new HashMap<String,Object>();
//			map.put("promotion", promotion);
//			//合并输出
//			template.process(map,new OutputStreamWriter(new FileOutputStream(htmlFile), "utf-8"));
			
//		}
//		// 存在 ，直接将文件返回
//				ServletActionContext.getResponse().setContentType(
//						"text/html;charset=utf-8");
//				FileUtils.copyFile(htmlFile, ServletActionContext.getResponse()
//						.getOutputStream());
//		return NONE;
//		  
//	}
	
	@Action(value = "promotion_Detail")
	public String showDetail() throws Exception {
		// 拿到模版文件生成的目标文件(html)的位置
		String htmlRealPath = ServletActionContext
				.getServletContext().getRealPath("/freemarker");
		File htmlFile = new File(htmlRealPath + "/" + 
				model.getId() + ".html");
		if (!htmlFile.exists()) {
			System.out.println(model.getId());
			// 生成配置文件
			Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
			// 生成模版对象
			configuration.setDirectoryForTemplateLoading(
					new File(ServletActionContext.getServletContext().getRealPath("/WEB-INF/freemarker_templates")));
			Template template = configuration.getTemplate("promotion_detail.ftl");
			// 获取动态数据
			Promotion promotion = WebClient                //services/promotionService
					.create(Constants.BOS_MANAGEMENT_URL+
							"/monkey_bos_management/services/promotionService/promotion/"
					+ model.getId()).accept(MediaType.APPLICATION_JSON).get(Promotion.class);
			System.out.println(promotion);
			Map<String,Object> result = new HashMap<>();
			result.put("promotion", promotion);
			//输出目标文件
			template.process(result, new OutputStreamWriter(new FileOutputStream(htmlFile)));
		}
		
		//存在的话直接输出
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		FileUtils.copyFile(htmlFile,ServletActionContext.getResponse().getOutputStream());
		return NONE;
	}

	
}
